package es.mgil.ejercicios.clase;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Dibujar extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(new RenderView(this));
	}

	private class RenderView extends View {
		private Paint paint;
		private int line_end_x;
		private int line_end_y;
		boolean up;

		@SuppressWarnings("deprecation")
		public RenderView(Context context) {
			super(context);
			paint = new Paint();
			line_end_x = getWindowManager().getDefaultDisplay().getWidth() - 1;
			line_end_y = 0;
			up = false;
		}

		@Override
		protected void onDraw(Canvas canvas) {
			// draw the background color
			canvas.drawRGB(255, 255, 255);

			{
				// draw a movable line
				if (line_end_x == canvas.getWidth() - 1) {
					if (up) {
						line_end_y--;
					} else {
						line_end_y++;
					}
				}

				if (line_end_y == canvas.getHeight() - 1) {
					if (up) {
						line_end_x++;
					} else {
						line_end_x--;
					}
				}

				if (line_end_x == 0 && line_end_y == canvas.getHeight() - 1) {
					up = true;
				} else if (line_end_x == canvas.getWidth() - 1
						&& line_end_y == 0) {
					up = false;
				}

				paint.setColor(Color.BLUE);
				canvas.drawLine(0, 0, line_end_x, line_end_y, paint);
			}

			{
				// draw a circle
				paint.setStyle(Style.FILL_AND_STROKE);
				paint.setColor(0xff00ff00);
				canvas.drawCircle(canvas.getWidth() / 2,
						canvas.getHeight() / 2, 10, paint);
			}

			{
				// draw a rectangle
				paint.setStyle(Style.FILL);
				paint.setColor(Color.RED);
				paint.setAlpha(0xcc);
				canvas.drawRect(50, 50, 100, 100, paint);
			}

			invalidate();
		}
	}
}
